﻿Imports System.Data.OleDb
Public Class editprofile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        If Not IsPostBack Then
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
            Dim sql As String = "SELECT * FROM Userprofile where dinerid=@a6"
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@a6", User.Identity.Name)
            conn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()


            dr.Read()
            If Not IsDBNull(dr("firstname")) Then TextBox1.Text = dr("firstname")
            If Not IsDBNull(dr("lastname")) Then TextBox2.Text = dr("lastname")
            If Not IsDBNull(dr("gender")) Then TextBox3.Text = dr("gender")
            'to get date of borth and convert it to string
            If Not IsDBNull(dr("dateofbirth")) Then
                TextBox4.Text = dr("dateofbirth")
            End If
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
       
        Dim newfilename As String = ""

        If FileUpload1.HasFile Then

            newfilename = Guid.NewGuid().ToString() & getsurfix(FileUpload1.FileName)
            If FileUpload1.HasFile Then
                FileUpload1.SaveAs(Server.MapPath("carpictures") & "/" & newfilename)
            End If
        End If
        Dim userdob As DateTime = TextBox4.Text

        Dim sql As String
        If FileUpload1.HasFile Then
            sql = "UPDATE userprofile SET firstname=@a1, lastname=@a2, portrait=@a3, dateofbirth=@a4, gender=@a5 where dinerid=@a6"
        Else
            sql = "UPDATE userprofile SET firstname=@a1, lastname=@a2, dateofbirth=@a4, gender=@a5 where dinerid=@a6"
        End If


        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@a1", TextBox1.Text)
        cmd.Parameters.AddWithValue("@a2", TextBox2.Text)
        If FileUpload1.HasFile Then
            cmd.Parameters.AddWithValue("@a3", "carpictures/" & newfilename)
        End If
        cmd.Parameters.AddWithValue("@a4", userdob.Date)
        cmd.Parameters.AddWithValue("@a5", TextBox3.Text)
        cmd.Parameters.AddWithValue("@a6", User.Identity.Name)

        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        MsgBox("Profile updated", MsgBoxStyle.Exclamation)
        Response.Redirect("profile.aspx?dinerid=" & User.Identity.Name)
    End Sub
    Function getsurfix(ByVal fn As String)
        Dim fileparts = fn.Split(".")
        Dim suffix = fileparts(fileparts.Length - 1)
        Return "." & suffix
    End Function
End Class